/*
 * To change this template, choose Tools | Templates
 * and start the template in the editor.
 */
package coursesmaritimes.obtention.server;

import java.net.*;
import java.io.*;

public class SocketServer {

    private Socket socket = null;
    private ServerSocket server = null;
    private DataInputStream streamIn = null;
    private DataOutputStream streamOut = null;
    public int serverPort;

    public SocketServer() {
        serverPort = 7500;
        try {
            server = new ServerSocket(serverPort);

        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }
// Accepte une demande client
    public void start() {
        try {
            socket = server.accept();
            streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
            streamOut = new DataOutputStream(socket.getOutputStream());
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }

    public void stop() {
        try {
            if (socket != null) {
                socket.close();
            }
            if (streamIn != null) {
                streamIn.close();
            }
            if (streamOut != null) {
                streamOut.close();
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage() + "Stop Server");
        }
    }

    public String getMessage() {
        String line = null;
        try {
            line = streamIn.readUTF();
        } catch (IOException ioe) {
            System.out.println(ioe.getMessage() + "getMessage Server");
        }
        return line;
    }

    public void sendMessage(String msg) {
        try {
            streamOut.writeUTF(msg);
            streamOut.flush();
        } catch (Exception ex) {
            System.out.println(ex.getMessage() + "sendMessage Server");
        }
    }
}
